package com.fhx.core.utils

import android.text.TextUtils
import android.util.Log
import java.lang.Exception
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


/**
 * author fhx
 * create time 2020/4/26 11:19
 **/
class DateFormatUtil {

    companion object {
        const val FORMAT_COMMON = "yyyy-MM-dd HH:mm:ss"
        fun dateEquals(beginTime: String): Boolean {
            val format = SimpleDateFormat(FORMAT_COMMON)
            val date1 = format.parse(beginTime)
            val currentDate = Date(System.currentTimeMillis())
            return date1.before(currentDate)
        }

        fun dateDifference(beginTime: String): Long {
            val format = SimpleDateFormat(FORMAT_COMMON)
            val date1 = format.parse(beginTime)
            val currentDate = Date(System.currentTimeMillis())
            return date1.time - currentDate.time
        }


        /**
         *oldDate 多种形式的值
         * pattern eg:yyyy-MM-dd HH:mm:ss
         */
        fun formatNew(oldDate: String?, pattern: String): String? {
            if (oldDate != null) {
                try {
                    if (oldDate.contains("T")) {
                        val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")//直接解析到秒
                        df.timeZone = TimeZone.getTimeZone("UTC")//带T的国际时间，用0时区解析
                        val date: Date = df.parse(oldDate)
                        val sdf = SimpleDateFormat(pattern)
                        return sdf.format(date)
                    } else if (oldDate.contains("-")) {//后端已转为中国时间，不处理了
                        val df = SimpleDateFormat(FORMAT_COMMON)//直接解析到秒
//                        df.timeZone = TimeZone.getTimeZone("UTC")
                        val date: Date = df.parse(oldDate)
                        val sdf = SimpleDateFormat(pattern)
                        return sdf.format(date)
                    } else {
                        if (oldDate.length < 11) {
                            //秒
                            return seconds2Date(oldDate, pattern)
                        } else {
                            //毫秒
                            return millis2Date(oldDate, pattern)
                        }
                    }

                } catch (e: Exception) {
                    return oldDate
                }
            }
            return oldDate
        }


        /**
         * 时间戳转换成日期格式字符串
         * @param seconds 精确到秒的字符串
         * @param formatStr
         * @return
         */
        private fun seconds2Date(seconds: String?, format: String?): String? {

            if (TextUtils.isEmpty(seconds)) {
                return ""
            }
            var formatx = format
            if (format == null || format.isEmpty()) {
                formatx = FORMAT_COMMON
            }
            val sdf = SimpleDateFormat(formatx)
            return sdf.format(Date(java.lang.Long.valueOf(seconds + "000")))
        }

        /**
         * 毫秒转时间
         */
        private fun millis2Date(millis: String?, format: String?): String? {

            if (TextUtils.isEmpty(millis)) {
                return ""
            }
            var formatx = format
            if (format == null || format.isEmpty()) {
                formatx = FORMAT_COMMON
            }
            val sdf = SimpleDateFormat(formatx)
            return sdf.format(Date(millis?.toLong()!!))
        }
    }
}